package com.ClasesGraficas;

import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import android.graphics.Color;

public class VentasN3
{
	private CategorySeries series = null;
	private DefaultRenderer mRenderer = null;
	private int [] colores = new int[] {Color.RED, Color.BLUE, Color.CYAN, Color.YELLOW,
			                            Color.GREEN, Color.LTGRAY, Color.MAGENTA,
			                            Color.RED, Color.BLUE, Color.CYAN, Color.YELLOW,
			                            Color.GREEN, Color.LTGRAY, Color.MAGENTA}; //Para tener colores definidos
	
	private double[] valores = null; //Debe ser la misma cantidad que los campos
	private String tituloGrafica = "";
	private String[] titulosCampos = null; //Debe ser la misma cantidad que los valores
	
	public void ejecutar(double [] valores, String tituloGrafica, String[] titulosCampos)
	{
		this.valores = valores;
		this.tituloGrafica = tituloGrafica;
		this.titulosCampos = titulosCampos;
		
		crearGrafica();
	}
	
	private void crearGrafica()
	{
		series = new CategorySeries(tituloGrafica);
		mRenderer = new DefaultRenderer();
		
		crearDataSet();
		crearRenderer();
		tuneRenderer();
	}
	
	private void crearDataSet()
	{
		double total = sumarValores(valores);
		
		if(valores !=null)
			for(int i=0;i<valores.length;i++)
				series.add(titulosCampos[i] + ": " + Math.round(((double)(valores[i])/(double)(total))*100) + "% (S/. " + valores[i] + ")", valores[i]);
	}
	
	private void crearRenderer()
	{
		for(int i=0;i<valores.length;i++)
		{
			SimpleSeriesRenderer r = new SimpleSeriesRenderer();
			r.setColor(colores[i]);
			r.setChartValuesSpacing(3);
			r.setChartValuesTextSize(30);
			
			mRenderer.addSeriesRenderer(r);
		}
	}
	
	private void tuneRenderer()
	{
		mRenderer.setChartTitle(tituloGrafica);
		mRenderer.setChartTitleTextSize(40);
		mRenderer.setLabelsColor(Color.WHITE);
		mRenderer.setLabelsTextSize(30);
		mRenderer.setShowLegend(true);
		mRenderer.setLegendTextSize(30);
		mRenderer.setLegendHeight(10);
	}
	
	private double sumarValores(double[] valores)
	{
		int valor = 0;
		for (int i=0;i<valores.length;i++)
			valor += valores[i];
		
		return valor;
	}
	
	/********************** GET/SET ************************/
	public CategorySeries getSeries() {return series;}
	public void setSeries(CategorySeries series) {this.series = series;}
	public DefaultRenderer getmRenderer() {return mRenderer;}
	public void setmRenderer(DefaultRenderer mRenderer) {this.mRenderer = mRenderer;}
}
